package com.zz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zz.common.R;
import com.zz.dto.PreplanDto;
import com.zz.dto.PublicityDto;
import com.zz.entity.Activity;
import com.zz.entity.Preplan;
import com.zz.entity.Publicity;
import com.zz.entity.User;
import com.zz.mapper.ActivityMapper;
import com.zz.mapper.PublicityMapper;
import com.zz.mapper.UserMapper;
import com.zz.service.PublicityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 朱彦燕
 */
@Service
@Validated
public class PublicityServiceImpl extends ServiceImpl<PublicityMapper, Publicity> implements PublicityService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PublicityMapper publicityMapper;

    @Autowired
    private ActivityMapper activityMapper;


    @Override
    public R<PublicityDto> info(int activityId) {
        // 条件查询器
        LambdaQueryWrapper<Publicity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Publicity::getActivityId, activityId);
        Publicity publicity = publicityMapper.selectOne(wrapper);

        if (publicity == null){
            return R.error("对不起，此活动安全预案为空！");
        }

        PublicityDto publicityDto = new PublicityDto();

        // 复制preplan给preplanDto
        BeanUtils.copyProperties(publicity, publicityDto);

        // Activity:条件查询器
        Activity activity = activityMapper.selectById(activityId);

        // User：条件查询器
        User user = userMapper.selectById(activity.getLeaderId());

        publicityDto.setLeaderName(user.getName());
        publicityDto.setLeaderPhone(user.getPhone());
        publicityDto.setActivityName(activity.getName());

        return R.success(publicityDto);
    }

    /**
        * `activity_id` BIGINT(20) NOT NULL COMMENT '活动编码',
       `leader_id` BIGINT(20) NOT NULL COMMENT '负责人学号',
       `method` VARCHAR(20)  NOT NULL COMMENT '宣传方式',
       `scheme` VARCHAR(500) NOT NULL COMMENT '宣传方案',
       `expected` VARCHAR(500) NOT NULL COMMENT '预期结果',
     */
    @Override
    public R<String> add(Publicity publicity, HttpServletRequest request) {
        // 条件查询器
        LambdaQueryWrapper<Publicity> wrapper = new LambdaQueryWrapper<>();
        // 活动编码
        wrapper.eq(Publicity::getActivityId, publicity.getActivityId());
        Publicity one = publicityMapper.selectOne(wrapper);

        // 活动编码重复
        if (one != null){
            return R.error("该活动宣传方案已存在！");
        }

        // 判断此活动是否存在
        LambdaQueryWrapper<Activity> activityWrapper = new LambdaQueryWrapper<>();
        activityWrapper.eq(Activity::getId, publicity.getActivityId());
        Activity activity = activityMapper.selectOne(activityWrapper);

        // 如果活动不存在
        if (activity == null){
            return R.error("该活动不存在！");
        }

        // 获取当前用户信息
        String userName = (String) request.getSession().getAttribute("user");
        // 条件查询器
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getName, userName);
        User user = userMapper.selectOne(userWrapper);

        // 设置本宣传方案负责人id为当前用户id
        publicity.setLeaderId(user.getId());

        // 将宣传方案插入到数据库
        publicityMapper.insert(publicity);

        return R.success("宣传方案增加成功！");
    }

    @Override
    public R<String> update(Publicity publicity) {
        // 条件查询器
        LambdaQueryWrapper<Publicity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Publicity::getActivityId, publicity.getActivityId());
        Publicity one = publicityMapper.selectOne(wrapper);

        // 如果此宣传方案不存在
        if (one == null){
            return R.error("目标修改的宣传方案不存在！");
        }

        // 同步更新到数据库
        publicityMapper.update(publicity, wrapper);

        return R.success("宣传方案修改成功！");
    }

    /**
    * preplan.activityId()
    * */
    @Override
    public R<String> delete(Publicity publicity) {
        // 条件查询器
        LambdaQueryWrapper<Publicity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Publicity::getActivityId, publicity.getActivityId());
        Publicity one = publicityMapper.selectOne(wrapper);

        if (one == null){
            return R.error("目标删除宣传方案不存在！");
        }

        // 从数据库中删除数据
        publicityMapper.delete(wrapper);

        return R.success("删除宣传方案成功！");

    }

//    @Override
//    public R<Page> page(int page, int pageSize, @Nullable String activityName, HttpServletRequest request) {
//        //构造分页构造器
//        Page<Publicity> pageInfo = new Page<>(page, pageSize);
//        Page<PublicityDto> publicityDtoPage = new Page<>(page, pageSize);
//
//        // 获取当前社团id
//        Long clubId = (Long) request.getSession().getAttribute("club");
//
//        // 条件查询器 模糊匹配 activityName
//        LambdaQueryWrapper<Activity> activityWrapper = new LambdaQueryWrapper<>();
//        activityWrapper.like(activityName != null, Activity::getName, activityName).eq(Activity::getClubId, clubId);
//        List<Activity> activityList = activityMapper.selectList(activityWrapper);
//
//        // 将所有符合条件的活动id放到数组里
//        ArrayList<Long> activityIds = new ArrayList<>();
//        for (Activity activity : activityList) {
//            activityIds.add(activity.getId());
//        }
//
//        // 构造条件构造器 activityIds
//        LambdaQueryWrapper<Publicity> queryWrapper = new LambdaQueryWrapper();
//        // 添加过滤条件(isNotEmpty(name))
//        queryWrapper.in(Publicity::getActivityId, activityIds);
//
//        //执行分页查询
//        //在page方法内部会将查好的数据封装，赋好值给pageInfo
//        publicityMapper.selectPage(pageInfo, queryWrapper);
//
//        //对象拷贝(除了records,records其实就是页面中一整行一整行数据)
//        BeanUtils.copyProperties(pageInfo, publicityDtoPage, "records");
//
//        List<Publicity> records = pageInfo.getRecords();
//        //遍历list集合对象,item即为获取到的一个个preplan对象
//        List<PublicityDto> list = records.stream().map((item) -> {
//            PublicityDto publicityDto = new PublicityDto();
//            // 将其Activity中的数据复制给DishDto
//            BeanUtils.copyProperties(item,publicityDto);
//
//            Long leaderId = item.getLeaderId();//负责人id
//            Long activityId = item.getActivityId();// 活动id
//            //根据id查询分类对象
//            User leader = userMapper.selectById(leaderId);
//            Activity activity = activityMapper.selectById(activityId);
//
//            if(leader != null){
//                publicityDto.setLeaderName(leader.getName());
//                publicityDto.setLeaderPhone(leader.getPhone());
//            }
//
//            if (activity != null) {
//                publicityDto.setActivityName(activity.getName());
//            }
//
//            return publicityDto;
//        }).collect(Collectors.toList());//把以上创建的dto对象都收集起来
//
//        publicityDtoPage.setRecords(list);
//
//        return R.success(publicityDtoPage);
//    }
}
